package com.huangjue.backend.service.impl;

import com.huangjue.backend.entity.Teacher;
import com.huangjue.backend.entity.User;
import com.huangjue.backend.entity.Student;
import com.huangjue.backend.dto.UserRegisterRequest;
import com.huangjue.backend.mapper.MessageMapper;
import com.huangjue.backend.mapper.StudentMapper;
import com.huangjue.backend.mapper.TeacherMapper;
import com.huangjue.backend.mapper.UserMapper;
import com.huangjue.backend.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private TeacherMapper teacherMapper;


    /*
    * 推荐老师
    * */
    @Override
    public List<Teacher> getRecommendTeacher(){return teacherMapper.getRecommendTeacher();}
    /**
     * 用户登录
     */
    @Override
    public User login(String email, String password) {
        return userMapper.selectByEmailAndPassword(email, password);
    }

    /**
     * 检查邮箱是否已存在
     */
    @Override
    public boolean isEmailExists(String email) {
        return userMapper.countByEmail(email) > 0;
    }
    
    /**
     * 用户注册
     */
    @Override
    @Transactional
    public boolean register(UserRegisterRequest registerRequest) {
        // 检查邮箱是否已存在
        if (isEmailExists(registerRequest.getEmail())) {
            return false;
        }
        // 1.创建用户对象
        User user = new User();
        user.setEmail(registerRequest.getEmail());
        user.setPassword(registerRequest.getPassword());
        user.setNickname(registerRequest.getNickname());
        user.setPicture("/uploads/touxiang/moren.png");
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        // 插入用户信息
        int userResult = userMapper.insert(user);
        //2.添加学生信息表
        int userId=user.getUserId();
        System.out.println(userId);
        Student student=new Student();
        student.setUserId(userId);
        int studentResult = studentMapper.insert(student);
        //3.注册成功，添加一条注册成功的通知
        int messageResult = messageMapper.welcomeMessage(userId);
        return userResult > 0 && studentResult > 0 && messageResult > 0;
    }

    @Override
    public User getUserById(Integer userId) {
        return userMapper.selectById(userId);
    }


    @Override
    public boolean changePicture(Integer userId, String url) {
        return userMapper.updateAvatar(userId, url) > 0;
    }

    @Override
    public boolean changePassword(Integer userId, String oldPassword, String newPassword) {
        User user = userMapper.selectById(userId);
        if (user != null && user.getPassword().equals(oldPassword)&& !user.getPassword().equals(newPassword)) {

            return userMapper.updatePassword(userId, newPassword) > 0;
        }
        return false;
    }

    @Override
    public boolean changeEmail(Integer userId, String newEmail, String password) {
        // 这里省略验证码校验逻辑
        return userMapper.updateEmail(userId, newEmail, password) > 0;
    }

    @Override
    public boolean sendVerificationCode(String email) {
        // 这里只做假实现
        return true;
    }

    @Override
    public boolean studentAuth(Student student) {
//        Student dbStudent = studentMapper.selectByUserId(student.getUserId());
        student.setIsCheck(0); // 0未审核
        student.setIsAuthenticate(1); // 0未认证
        student.setUpdateTime(LocalDateTime.now());
        return studentMapper.update(student) > 0;

    }

    @Override
    public boolean updateUsername(User user) {
        return userMapper.updateUsername(user.getUserId(),user.getNickname()) > 0;
    }

    @Override
    public Teacher getTeacherById(Integer teacherId) {
        return teacherMapper.getTeacherById(teacherId);
    }
    
    @Override
    public Integer getTeacherIdByRecommendId(Integer recommendId) {
        return teacherMapper.getTeacherIdByRecommendId(recommendId);
    }

    @Override
    public boolean sendFlowerToTeacher(Integer teacherId) {
        return teacherMapper.updateTeacherFlower(teacherId) > 0;
    }
} 